package view.timeline;

import java.awt.Dimension;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import constants.Icons;
import controller.CompleteListener;



import view.BaseComponent;

public class TimeJumperPanel extends BaseComponent implements KeyListener, FocusListener {

	private JTextField min;
	private JTextField sec;
	private JButton go;
	
	public TimeJumperPanel() {
		JPanel row = new JPanel();
		
		min = new JTextField("0",1);
		sec = new JTextField("00",2);
		go = new JButton(Icons.getIcon("goto"));
		
		go.setActionCommand("go");
		go.setToolTipText("Places the time cursor \n"+
				"at the required time.");
		
//		row.setLayout(new BoxLayout(row, BoxLayout.LINE_AXIS));
		this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
		this.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createLoweredBevelBorder(),
				BorderFactory.createEmptyBorder(3,3,3,3)));
		
//		row.add(min);
//		row.add(new JLabel(" : "));
//		row.add(sec);
//		row.add(new JLabel("."));
		
		this.add(min);
		this.add(new JLabel(" : "));
		this.add(sec);
		this.add(Box.createHorizontalStrut(5));
		this.add(go);
		this.add(Box.createHorizontalGlue());
		
		min.setMaximumSize(new Dimension(20,20));
		sec.setMaximumSize(new Dimension(30,20));
		
		min.setMinimumSize(new Dimension(20,20));
		sec.setMinimumSize(new Dimension(30,20));
		
		min.addKeyListener(this);
		min.addFocusListener(this);
		sec.addKeyListener(this);
		sec.addFocusListener(this);
		
//		go.setMaximumSize(new Dimension(60,20));
		go.setMaximumSize(new Dimension(go.getIcon().getIconWidth()+4,
				go.getIcon().getIconHeight()+4));
		go.setPreferredSize(new Dimension(go.getIcon().getIconWidth()+4,
				go.getIcon().getIconHeight()+4));
		
		this.setPreferredSize(new Dimension(90,35));
		this.setMaximumSize(new Dimension(90,35));
	}
	
	public int getTime() {
		int ret;
		try {
			ret = 60*Integer.parseInt(min.getText())
		+Integer.parseInt(sec.getText());
		}
		catch (NumberFormatException e) {
			min.setText("0");
			sec.setText("00");
			return 0;
		}
		return ret;
	}
	
	@Override
	public void attachListener(CompleteListener l) {
		go.addActionListener(l);
	}

	@Override
	public void setEnabled(boolean enable) {
		min.setEnabled(enable);
		sec.setEnabled(enable);
		go.setEnabled(enable);
	}

	public void keyTyped(KeyEvent e) {
		if (e.getKeyChar()=='\n')
			go.doClick();
	}

	public void keyPressed(KeyEvent e) {}
	public void keyReleased(KeyEvent e) {}

	public void focusGained(FocusEvent e) {
		((JTextField)e.getSource()).selectAll();
	}

	public void focusLost(FocusEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
